package com.soft.comprehensiveexercise.game;

import java.util.Scanner;

/**
 * @author yzg
 * @desc TODO
 * @date 2025/4/18
 */
public class FIR {
    private static final char EMPTY = '*';
    private static final char BLACK = '@';
    private static final char WHITE = 'O';

    private char[][] board;
    private int size;
    private boolean isBlackTurn;

    public FIR(int size) {
        this.size = size;
        this.board = new char[size][size];
        this.isBlackTurn = true;
        initializeBoard();
    }

    private void initializeBoard() {
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                board[i][j] = EMPTY;
            }
        }
    }

    private void printBoard() {
        // 打印行字母和棋盘
        char rowLabel = 'a';
        for (int i = 0; i < size; i++) {
            System.out.print(rowLabel + " ");
            for (int j = 0; j < size; j++) {
                System.out.print(board[i][j] + " ");
            }
            System.out.println();
            rowLabel++;
        }

        // 打印列字母
        System.out.print("  ");
        for (int i = 0; i < size; i++) {
            System.out.print((char)('a' + i) + " ");
        }
        System.out.println();
    }

    private boolean makeMove(String move) {
        if (move.length() != 2) {
            return false;
        }

        int row = move.charAt(0) - 'a';
        int col = move.charAt(1) - 'a';

        if (row < 0 || row >= size || col < 0 || col >= size || board[row][col] != EMPTY) {
            return false;
        }

        board[row][col] = isBlackTurn ? BLACK : WHITE;
        return true;
    }

    private boolean checkWin(int row, int col) {
        char player = board[row][col];//从棋盘 board 中取出当前落子的位置 (row, col) 的棋子（@ 或 O）

        int[][] directions = {{0, 1}, {1, 0}, {1, 1}, {1, -1}};// {0, 1}：水平向右（列增加，行不变）,{1, 0}：垂直向下（行增加，列不变）,{1, 1}：右下对角线（行和列同时增加）,{1, -1}：左下对角线（行增加，列减少）

        for (int[] dir : directions) {//对每个方向dir检查该方向上是否有五子连珠
            int count = 1;

            // 正向检查
            for (int i = 1; i < 5; i++) {
                int r = row + i * dir[0];
                int c = col + i * dir[1];
                if (r < 0 || r >= size || c < 0 || c >= size || board[r][c] != player) {
                    break;
                }
                count++;
            }

            // 反向检查
            for (int i = 1; i < 5; i++) {
                int r = row - i * dir[0];
                int c = col - i * dir[1];
                if (r < 0 || r >= size || c < 0 || c >= size || board[r][c] != player) {
                    break;
                }
                count++;
            }

            if (count >= 5) {
                return true;
            }
        }

        return false;
    }

    public void startGame() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("即将开局");
        printBoard();

        while (true) {
            System.out.println(isBlackTurn ? "请黑方下子：" : "请白方下子：");
            String move = scanner.next().trim().toLowerCase();

            if (!makeMove(move)) {
                System.out.println("无效的落子位置，请重新输入！");
                continue;
            }

            printBoard();

            int row = move.charAt(0) - 'a';
            int col = move.charAt(1) - 'a';
            if (checkWin(row, col)) {
                System.out.println((isBlackTurn ? "黑方" : "白方") + "获胜！");
                break;
            }

            isBlackTurn = !isBlackTurn;
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入棋盘的大小，至少为5：");
        int size = scanner.nextInt();

        if (size < 5) {
            System.out.println("棋盘大小至少为5！");
            return;
        }

        FIR game = new FIR(size);
        game.startGame();
    }
}
